<template>
  <div class="pro-line">
    <div>
      <label>{{ $t("route.Maintenance_Management") }}</label>
      <div></div>
      <button>
        <img /><label>{{ $t("route.Purchase_parts") }}</label>
      </button>
    </div>

    <div>
      <div>
        <!-- <el-select
          @change="selectChange1"
          style="margin-left: 10px"
          v-model="query.factory"
          placeholder="请选择工厂"
        >
          <el-option v-for="item in factoryList" :key="item" :label="item" :value="item">
          </el-option>
        </el-select> -->
        <el-select
          @change="selectChange1"
          style="margin-left: 10px"
          v-model="query.workshop"
          :placeholder="$t('route.Please_select_the_type')"
        >
          <el-option v-for="item in workshopList" :key="item" :label="item" :value="item">
          </el-option>
        </el-select>
        <div class="div-handle-right">
          <el-button @click="searchClick">{{ $t("route.search") }}</el-button>
          <input v-model="query.search" :placeholder="$t('route.Head_Model')" />
        </div>
      </div>

      <el-table class="table" :data="partList" style="width: 100%">
        <el-table-column prop="kind" :label="$t('route.Device_category')" />
        <el-table-column prop="noseCode" :label="$t('route.Head_Model')" />
        <el-table-column prop="code" :label="$t('route.Part_No')" />
        <el-table-column prop="name" :label="$t('route.designation')" />
        <el-table-column prop="batch" :label="$t('route.batch')" />
        <el-table-column prop="num" :label="$t('route.amount')" />
        <el-table-column prop="price" :label="$t('route.price')" />
        <el-table-column :label="$t('route.Replace_or_not')">
          <template #default="scope">
            {{ scope.row.rpc ? $t("route.yes") : $t("route.no") }}
          </template>
        </el-table-column>
        <el-table-column prop="nickname" :label="$t('route.applicant')" />
        <el-table-column :label="$t('route.applicant')" width="80">
          <template #default="scope">
            {{ scope.row.workshop ? scope.row.workshop : scope.row.store }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('route.location')">
          <template #default="scope">
            <el-button
              @click="deleteClick(scope.row.id)"
              style="color: #ff0000"
              type="text"
              >{{ $t("route.Delete") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          style="float: right"
          background
          @current-change="handlePageChange"
          :current-page="query.pageNum"
          :page-size="query.pageSize"
          layout="total, prev, pager, next"
          :total="query.total"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import http from "../../js/http/http.js";
import api from "../../js/http/api.js";
import CommonUtils from "../../js/common/common-utils.js";
import i18n from "../../i18n/index";
import Constant from "../../js/common/constant.js";
export default {
  data() {
    return {
      user: null,
      query: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        search: null,
        workshop: i18n.global.t("route.All_workshops"),
      },
      partList: [],
      workshopList: [],
      factoryList: [],
    };
  },
  methods: {
    searchClick() {
      this.query.workshop = i18n.global.t("route.All_workshops");
      this.query.pageNum = 1;
      this.getPartList();
    },
    selectChange1() {
      this.query.pageNum = 1;
      this.getPartList();
    },
    getPartList() {
      http.post(
        api.GET_PART_LIST1,
        {
          search: this.query.search,
          pageNum: this.query.pageNum,
          pageSize: this.query.pageSize,
          adminId: this.$store.state.user.id,
          factory: Constant.FACTORY == "全部工厂" ? null : Constant.FACTORY,
          workshop:
            this.query.workshop == i18n.global.t("route.All_workshops")
              ? null
              : this.query.workshop,
        },
        (data) => {
          this.partList = data.data.partList;
          this.query.total = data.data.total;
        }
      );
    },
    deleteClick(partId) {
      this.$confirm(i18n.global.t("route.continue11"), i18n.global.t("route.reminder"), {
        confirmButtonText: i18n.global.t("route.sure"),
        cancelButtonText: i18n.global.t("route.cancel"),
        type: "warning",
      }).then(() => {
        http.post(
          api.DELETE_PART,
          {
            partId: partId,
          },
          (data) => {
            this.$message.success(i18n.global.t("route.Delete_succeeded"));
            this.getPartList();
          }
        );
      });
    },
    getAllWorkshop() {
      http.post(
        api.GET_ALL_WORKSHOP,
        {
          adminId: this.user.id,
          factory: Constant.FACTORY == "全部工厂" ? null : Constant.FACTORY,
        },
        (data) => {
          this.workshopList.push(i18n.global.t("route.All_workshops"));
          for (var item of data.data) {
            this.workshopList.push(item.name);
          }
        }
      );
    },
    getAllFactory() {
      http.post(
        api.GET_ALL_FACTORY,
        {
          adminId: this.user.id,
        },
        (data) => {
          this.factoryList.push("全部工厂");
          for (var item of data.data) {
            this.factoryList.push(item.name);
          }
        }
      );
    },
    handlePageChange(pageNum) {
      this.query.pageNum = pageNum;
      this.getPartList();
    },
  },
  created() {
    this.user = this.$store.state.user;
    this.getPartList();
    this.getAllWorkshop();
    this.getAllFactory();
  },
};
</script>
<style lang="scss" scoped>
.pro-line {
  color: #444444;
  font-size: 18px;
  padding-right: 20px;
  height: calc(100vh - 100px);
  // overflow: scroll;
  scrollbar-width: none;
  /* firefox */
  -ms-overflow-style: none;
  /* IE 10+ */
  overflow-x: hidden;
  overflow-y: auto;

  > div {
    display: flex;
    flex-direction: row;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;
  }

  > div:nth-of-type(1) {
    > label:nth-of-type(1) {
      font-weight: 600;
    }

    > div:nth-of-type(1) {
      width: 1px;
      background-color: #cccccc;
      height: 15px;
      margin: 0px 20px;
    }

    > button:nth-of-type(1) {
      background-image: url(../../assets/img/customer-1.png);
      background-repeat: no-repeat;
      background-size: 100% 100%;
      padding: 5px 10px;
      border: 0px;
      display: flex;
      color: #ffffff;
      flex-direction: row;
      align-items: center;
      font-weight: 800;

      > img {
        margin-right: 7px;
        width: 15px;
        height: 15px;
        content: url(../../assets/img/customer-2.png);
      }
    }
  }

  > div:nth-of-type(2) {
    flex-direction: column;

    > div:nth-of-type(1) {
      display: flex;
      flex-direction: row;
      align-items: center;

      .div-handle-right {
        flex: 1;
        flex-direction: row-reverse;
        text-align: right;
        display: flex;
        align-items: center;

        > input:nth-of-type(1) {
          border: 1px solid #eeeeee;
          font-size: 15px;
          width: 150px;
          padding: 5px 8px;
          outline: none;
        }

        > button {
          color: #a866ff;
          margin-left: 10px;
          padding: 4px 20px;
        }
      }
    }

    .table {
      margin-top: 20px;
    }
  }
}
</style>
